package svgedit.gui.actions;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToggleButton;

import svgedit.gui.Frame;

/**
 *  To monitor when translate to German language
 * */
@SuppressWarnings("serial")
public class GermanLanAction extends AbstractAction{
	private Frame frame;
	
	/**  Resource Bundle for current user's locale */
    private ResourceBundle srcBundle;
	
    /** Create this action for the given frame */
    public GermanLanAction(Frame frame) {
        super("German");
        this.frame = frame;
    }

    /** To translate all the text on each component
     * 
     * @param ae to monitor mouse event
     * */
    @SuppressWarnings("unchecked")
	public void actionPerformed(ActionEvent ae) {
       
    	// Get the resource bundle and point it to German property
    	srcBundle = ResourceBundle.getBundle( 
                "Locale_ge", 
                frame.getLocales()[1]);
    	Set set = frame.getCompos().entrySet();
    	Iterator it = set.iterator();
    	// Set all texts on the components to the language that it is selected
    	while (it.hasNext()){
    		Map.Entry me = (Map.Entry)it.next();
    		String key = (String)me.getKey();
    		if (key.split(" ")[0].equalsIgnoreCase("menu")){
    			JMenu menu = (JMenu)me.getValue();
    			menu.setText(srcBundle.getString(key.split(" ")[1]));
    		}
    		else if (key.split(" ")[0].equalsIgnoreCase("menuItem")){
    			JMenuItem menuItem = (JMenuItem)me.getValue();
    			menuItem.setText(srcBundle.getString(key.split(" ")[1]));
    		}
    		else if (key.startsWith("JToggleButton")){
    			JToggleButton button = (JToggleButton)me.getValue();
    			button.setText(srcBundle.getString(key.split(" ")[1]));
    		}
    		else if (key.startsWith("label")){
    			JLabel label = (JLabel)me.getValue();
    			label.setText(srcBundle.getString(key.split(" ")[1]));
    		}
    		
    	}
    	
    }
}
